﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;

namespace LIVITECH_ModbusDevLib
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Please select test function:\n1 :Rectangle Scan Device Test.\n2 :Optical Delay Line Device Test.\nOther: Exit.");
            string sel = Console.ReadLine();
            int testtype = 0;
            if (Int32.TryParse(sel,out testtype))
            {
                switch (testtype)
                {
                    case 1:
                        rectScanDevTest();
                        break;
                    case 2:
                        ODLtest();
                        break;
                    default:
                        return;
                }
            }
            else
            {
                Console.WriteLine("Exit...");
                return;
            }

        }

        private static void rectScanDevTest()
        {
            Console.WriteLine("Rect scan device test");
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
            Console.WriteLine("");
            Console.WriteLine("Please input device port! Empty=COM1");
            string port = Console.ReadLine();
            if (string.IsNullOrEmpty(port))
            {
                Console.WriteLine("Device port = COM1");
                port = "COM1";
            }
            port = port.ToUpper();
            string pattern = @"^COM[0-9]*$";
            if (!Regex.IsMatch(port, pattern))
            {
                Console.WriteLine("Invalid prot name!");
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("");

            Console.WriteLine("Please input device ID! Empty=1");
            string slaveIDstr = Console.ReadLine();
            if (string.IsNullOrEmpty(slaveIDstr))
            {
                Console.WriteLine("Device ID = 1");
                slaveIDstr = "1";
            }
            pattern = @"^[0-9]*$";
            if (!Regex.IsMatch(slaveIDstr, pattern))
            {
                Console.WriteLine("Invalid device ID!,change to 1");
                slaveIDstr = "1";
            }
            int slaveID = Convert.ToInt32(slaveIDstr);


            Console.WriteLine("");

            Console.WriteLine("Please input device baudrate! Empty=19200");
            string baudrateStr = Console.ReadLine();
            if (string.IsNullOrEmpty(baudrateStr)) baudrateStr = "19200";
            if (string.IsNullOrEmpty(baudrateStr))
            {
                Console.WriteLine("Device baudrate = 19200");
                baudrateStr = "19200";
            }
            pattern = @"^[0-9]*$";
            if (!Regex.IsMatch(baudrateStr, pattern))
            {
                Console.WriteLine("Invalid device baudrate!,change to 19200");
                baudrateStr = "19200";
            }
            int baudrate = Convert.ToInt32(baudrateStr);

            RectScanDev dev = new RectScanDev(port, baudrate, slaveID);
            string errstr;
            int res = dev.OpenDevice(out errstr);
            if (res != 0)
            {
                Console.WriteLine("Open device failed");
                Console.WriteLine("Press any key to continue...\n");
                dev = null;
                Console.ReadKey();
                return;
            }

            Console.WriteLine("");

            if (!dev.IsDeviceValid)
            {
                dev.CloseDevice();
                dev = null;
                Console.WriteLine("Device Modbus communication failed!");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Open device success!");
            Console.WriteLine("Press any key to continue...\n");
            Console.ReadKey();

            Console.WriteLine("Red Laser On test.");
            Console.WriteLine("Press any key to continue...\n");
            Console.ReadKey();
            dev.RedLaserOnOff(true);

            Console.WriteLine("Red Laser Off test.");
            Console.WriteLine("Press any key to continue...\n");
            Console.ReadKey();
            dev.RedLaserOnOff(false);

            Console.WriteLine("Red Laser On test.");
            Console.WriteLine("Press any key to continue...\n");
            Console.ReadKey();
            dev.RedLaserOnOff(true);

            Console.WriteLine("Set device parameters test.");
            Console.WriteLine("Press any key to continue...\n");
            Console.ReadKey();
            dev.param.probeType = 1;
            dev.param.scanShape = 2;
            dev.param.x_stepPerRange = 300;
            dev.param.x_freq_div = 4;
            dev.param.x_start_v = 0.95f;
            dev.param.x_end_v = -0.95f;
            dev.param.y_stepPerRange = 600;
            dev.param.y_start_v = 0.7f;
            dev.param.y_end_v = 0.8f;
            dev.param.y_AmpScale = 0.9f;
            dev.param.wave_phase_shift = 10;
            dev.UpdateParamsToDev();

            Console.WriteLine("Reset Rect Position test.");
            Console.WriteLine("Press any key to continue...\n");
            Console.ReadKey();
            dev.ResetRectPos();

            Console.WriteLine("Clear device parameters test.");
            Console.WriteLine("Press any key to continue...\n");
            Console.ReadKey();
            dev.param.probeType = 0;
            dev.param.scanShape = 0;
            dev.param.x_stepPerRange = 0;
            dev.param.x_freq_div = 0;
            dev.param.x_start_v = 0;
            dev.param.x_end_v = 0;
            dev.param.y_stepPerRange = 0;
            dev.param.y_start_v = 0;
            dev.param.y_end_v = 0;
            dev.param.y_AmpScale = 0;
            dev.param.wave_phase_shift = 0;
            dev.UpdateParamsToDev();

            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("Check ProbeType Signal test.Time" + (i + 1));
                Console.WriteLine("Press any key to continue...\n");
                Console.ReadKey();
                Console.WriteLine("ProbeType Signal = " + dev.ProbeTypeSignal);
            }

            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("Check Capture Button Signal test.Time" + (i + 1));
                Console.WriteLine("Press any key to continue...\n");
                Console.ReadKey();
                Console.WriteLine("Capture Button Signal = " + dev.CaptureButtonSignal);
            }

            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("Video capture Button Signal test.Time" + (i + 1));
                Console.WriteLine("Press any key to continue...\n");
                Console.ReadKey();
                Console.WriteLine("Video capture Button = " + dev.VideoCapButtonSignal);
            }


            Console.WriteLine("Test finish.Press any key to exit...");
            Console.ReadKey();

            dev.CloseDevice();
            dev = null;
        }

        private static void ODLtest()
        {
            Console.WriteLine("Optical delay device test");
            Console.WriteLine("Press any key to continue...");

            Console.ReadKey();
            Console.WriteLine("");
            Console.WriteLine("Please input device port! Empty=COM2");
            string port = Console.ReadLine();
            if (string.IsNullOrEmpty(port))
            {
                Console.WriteLine("Device port = COM2");
                port = "COM2";
            }
            port = port.ToUpper();
            string pattern = @"^COM[0-9]*$";
            if (!Regex.IsMatch(port, pattern))
            {
                Console.WriteLine("Invalid prot name!");
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("");

            Console.WriteLine("Please input device ID! Empty=2");
            string slaveIDstr = Console.ReadLine();
            if (string.IsNullOrEmpty(slaveIDstr))
            {
                Console.WriteLine("Device ID = 2");
                slaveIDstr = "2";
            }
            pattern = @"^[0-9]*$";
            if (!Regex.IsMatch(slaveIDstr, pattern))
            {
                Console.WriteLine("Invalid device ID!,change to 1");
                slaveIDstr = "1";
            }
            int slaveID = Convert.ToInt32(slaveIDstr);


            Console.WriteLine("");

            Console.WriteLine("Please input device baudrate! Empty=19200");
            string baudrateStr = Console.ReadLine();
            if (string.IsNullOrEmpty(baudrateStr)) baudrateStr = "19200";
            if (string.IsNullOrEmpty(baudrateStr))
            {
                Console.WriteLine("Device baudrate = 19200");
                baudrateStr = "19200";
            }
            pattern = @"^[0-9]*$";
            if (!Regex.IsMatch(baudrateStr, pattern))
            {
                Console.WriteLine("Invalid device baudrate!,change to 19200");
                baudrateStr = "19200";
            }
            int baudrate = Convert.ToInt32(baudrateStr);

            OpticalDelayDev dev = new OpticalDelayDev(port, baudrate, slaveID);
            string errstr;
            int res = dev.OpenDevice(out errstr);
            if (res != 0)
            {
                Console.WriteLine("Open device failed");
                Console.WriteLine("Press any key to continue...\n");
                dev = null;
                Console.ReadKey();
                return;
            }

            Console.WriteLine("");

            if (!dev.IsDeviceValid)
            {
                dev.CloseDevice();
                dev = null;
                Console.WriteLine("Device Modbus communication failed!");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }

            dev.stop();
            Thread.Sleep(200);

            Console.WriteLine("ODL status info = " + dev.statusInfo);
            Console.WriteLine("ODL fault info = " + dev.faultInfo);
            Console.WriteLine("ODL current position = " + dev.positionPulse);
            Console.WriteLine("ODL current enc position = " + dev.positionEncoder);
            Console.WriteLine("");


            bool go_neg = false;
            int cnt = 100;
            Stopwatch watch = new Stopwatch();

            watch.Start();
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("ODL Jog test!\n Press '1' Jog negative, other positive. ");
                dev.jogTimeLimit = 150;
                ConsoleKey key = Console.ReadKey().Key;
                go_neg = (key == ConsoleKey.NumPad1 || key == ConsoleKey.D1);
                Console.WriteLine("Press any key to continue...\n");
                cnt = 100;
                watch.Restart();

                while (cnt-- > 0 && watch.ElapsedMilliseconds < 1000)
                {
                    dev.jog(go_neg);
                    Thread.Sleep(10);
                    Console.WriteLine("ODL status info = " + dev.statusInfo);
                }


                dev.stop();
                Console.WriteLine("ODL status info = " + dev.statusInfo);
                Console.WriteLine("ODL fault info = " + dev.faultInfo);
                Console.WriteLine("ODL current position = " + dev.positionPulse);
                Console.WriteLine("ODL current enc position = " + dev.positionEncoder);
                Console.WriteLine("");
            }
            
            watch.Stop();

            Console.WriteLine("ODL home test!");
            Console.WriteLine("Press any key to continue...\n");
            Console.ReadKey();
            dev.stop();
            Thread.Sleep(200);
            dev.home();
            Console.WriteLine("ODL status info = " + dev.statusInfo);
            Console.WriteLine("Please wait to finish.");
            Console.WriteLine("Press any key to continue...\n");
            Console.ReadKey();

            dev.stop();
            Thread.Sleep(200);
            Console.WriteLine("ODL status info = " + dev.statusInfo);
            Console.WriteLine("ODL fault info = " + dev.faultInfo);
            Console.WriteLine("ODL current position = " + dev.positionPulse);
            Console.WriteLine("ODL current enc position = " + dev.positionEncoder);
            Console.WriteLine("");


            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("ODL Encoder Position Go test!");
                Console.WriteLine("Press any key to continue...\n");
                Console.WriteLine("Please Input Destination !\n Empty mean default value. \n Not interger mean exit.");
                string s = Console.ReadLine();
                int pos = 0;
                if (string.IsNullOrEmpty(s))
                {
                    switch (i)
                    {
                        case 0:
                            s = "5000";
                            break;
                        case 1:
                            s = "20000";
                            break;
                        case 2:
                            s = "0";
                            break;
                    }
                }

                bool isNum = Int32.TryParse(s, out pos);
                if (!isNum) break;

                dev.encAbsGo(pos);
                Console.WriteLine("Please wait to finish.");
                Console.ReadKey();

                Console.WriteLine("ODL status info = " + dev.statusInfo);
                Console.WriteLine("ODL fault info = " + dev.faultInfo);
                Console.WriteLine("ODL current position = " + dev.positionPulse);
                Console.WriteLine("ODL current enc position = " + dev.positionEncoder);
                Console.WriteLine("Press any key to continue...\n");

            }

            dev.stop();
            Thread.Sleep(200);

            dev.CloseDevice();
            dev = null;

            Console.WriteLine("Test finish.Press any key to exit...");
            Console.ReadKey();
        }
    }
}
